﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows.Forms;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraEditors;
using DevExpress.XtraNavBar;
using DevExpress.XtraSplashScreen;

namespace NAGFwk.UI {
    public class ModulesNavigator {
        readonly RibbonControl _ribbon;
        readonly PanelControl _panel;
        public ModulesNavigator(RibbonControl ribbon, PanelControl panel) {
            _ribbon = ribbon;
            _panel = panel;
        }
        public void ChangeSelectedItem(NavBarItemLink link, object moduleData) {
            bool allowSetVisiblePage = true;
            NavBarGroupTagObject groupObject = link.Item.Tag as NavBarGroupTagObject;
            if (groupObject == null)
                return;
            List<RibbonPage> deferredPagesToShow = new List<RibbonPage>();
            foreach (RibbonPage page in _ribbon.Pages) {
                if (!string.IsNullOrEmpty(string.Format("{0}", page.Tag))) {
                    bool isPageVisible = groupObject.Name.Equals(page.Tag);
                    if (isPageVisible != page.Visible && isPageVisible)
                        deferredPagesToShow.Add(page);
                    else
                        page.Visible = isPageVisible;
                }
                if (page.Visible && allowSetVisiblePage) {
                    //page.Text = "Home";
                    _ribbon.SelectedPage = page;
                    allowSetVisiblePage = false;
                }
            }
            bool firstShow = groupObject.Module == null;
            if (firstShow) {                
                if (SplashScreenManager.Default == null)
                    SplashScreenManager.ShowForm(_ribbon.FindForm(), typeof(WfMain), false, true);
                ConstructorInfo constructorInfoObj = groupObject.ModuleType.GetConstructor(Type.EmptyTypes);
                if (constructorInfoObj != null) {
                    groupObject.Module = constructorInfoObj.Invoke(null) as BaseModule;
                    groupObject.Module.InitModule(_ribbon, moduleData);
                }
                if (SplashScreenManager.Default != null)
                {
                    Form frm = moduleData as Form;
                    if (frm != null)
                    {
                        if (SplashScreenManager.FormInPendingState)
                            SplashScreenManager.CloseForm();
                        else
                            SplashScreenManager.CloseForm(false, 500, frm);
                    }
                    else
                        SplashScreenManager.CloseForm();
                }
            }

            foreach (RibbonPage page in deferredPagesToShow) {
                page.Visible = true;
            }
            foreach (RibbonPage page in _ribbon.Pages) {
                if (page.Visible) {
                    _ribbon.SelectedPage = page;
                    break;
                }
            }

            if (groupObject.Module != null) {
                if (_panel.Controls.Count > 0) {
                    BaseModule currentModule = _panel.Controls[0] as BaseModule;
                    if (currentModule != null)
                        currentModule.HideModule();
                }
                _panel.Controls.Clear();
                _panel.Controls.Add(groupObject.Module);
                groupObject.Module.Dock = DockStyle.Fill;
                groupObject.Module.ShowModule(firstShow);
            }
        }
        public BaseModule CurrentModule {
            get {
                if (_panel.Controls.Count == 0)
                    return null;
                return _panel.Controls[0] as BaseModule;
            }
        }
    }
}
